﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace CreatePGSQLDataScript
{
    public partial class K1 : Form
    {
        public K1()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            string tableName = "dailyequ";
            string sqlText = "select * from " + tableName + " where 1=2";

            SqlDataAdapter sql = new SqlDataAdapter(txtConnection.Text, sqlText);
            DataSet ds = new DataSet();
            sql.Fill(ds);
            DataTable dt= ds.Tables[0];


            List<string> Keys = getKey(dt);

            string keys = "";

            string k1 = "";
            foreach (string k in Keys)
            {
                k1 += string.Format("and a.{0}=b.{0} collate Chinese_Taiwan_Stroke_CI_AS ", k);

                keys += ","+k;
            }
            if (keys != "")
                keys = keys.Substring(1);



            string sqlText2 = @"

select distinct * into tempTable from allnaturelasted..dailyequ as a  
where 1=1 
--and a.ddate >'2011/06/01'
and not exists(
select 1 from  allnaturelasted0617..dailyequ as b 
where 1=1
and a.ddate=b.ddate collate Chinese_Taiwan_Stroke_CI_AS 
and a.father=b.father collate Chinese_Taiwan_Stroke_CI_AS
and a.son=b.son collate Chinese_Taiwan_Stroke_CI_AS
)
--order by a.ddate,a.father,a.son;
";



        }



        private List<string> getKey(DataTable dt)
        {
            List<string> Keys = new List<string>();
            foreach (var dc in dt.PrimaryKey)
            {
                Keys.Add(dc.ColumnName);
            }
            return Keys;
        }


    }
}
